package com.ezcoding.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;

public class Tools {
    public static boolean checkParaIsInValidate(String... paras) {
        for (int i = 0; i < paras.length; i++) {
            String para = paras[i];
            if (para == null || "".equals(para)) {
                System.out.println("Parameter is invalid at index:" + i);
                return true;
            }
        }

        return false;
    }

    public static boolean checkParaIsNullPoint(String... paras) {
        for (int i = 0; i < paras.length; i++) {
            String para = paras[i];
            if (para == null) {
                System.out.println("Parameter is null at index:" + i);
                return true;
            }
        }

        return false;
    }

    /**
     * 文字转拼音
     *
     * @param ChineseLanguage 要转换的中文
     * @return 拼音结果
     */
    public static String toHanyuPinyin(String ChineseLanguage) {

        if (ChineseLanguage == null) {
            ChineseLanguage = "null";
        }
        char[] cl_chars = ChineseLanguage.trim().toCharArray();
        StringBuilder hanyupinyin = new StringBuilder();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);// 输出拼音全部小写
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// 不带声调
        defaultFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        try {
            for (char c : cl_chars) {
                if (String.valueOf(c).matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音
                    hanyupinyin.append(PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat)[0]);
                } else {// 如果字符不是中文,则不转换
                    hanyupinyin.append(c);
                }
            }
            hanyupinyin.append("-");
            for (char c : cl_chars) {
                if (String.valueOf(c).matches("[\u4e00-\u9fa5]+")) {// 如果字符是中文,则将中文转为汉语拼音
                    hanyupinyin.append(PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat)[0].substring(0, 1));
                } else {// 如果字符不是中文,则不转换
                    hanyupinyin.append(c);
                }
            }
        } catch (BadHanyuPinyinOutputFormatCombination e) {
            System.out.println("字符不能转成汉语拼音");
        }

        return hanyupinyin.toString();
    }

    public static int parseNatureInteger(String valueStr) {
        int value;
        try {
            value = Integer.parseInt(valueStr);
        } catch (NumberFormatException e) {
            value = -1;
            e.printStackTrace();
        }

        return value;
    }

    public static double parseNonNegativeDouble(String valueStr) {
        double value;
        try {
            value = Double.parseDouble(valueStr);
        } catch (NumberFormatException e) {
            value = -1;
            e.printStackTrace();
        }

        return value;
    }

    public static String getRandomId() {
        BigInteger bigInteger = new BigInteger(System.currentTimeMillis() + "", 10);

        return bigInteger.toString(16).toUpperCase();
    }

    public static double formatDouble(double value) {
        BigDecimal bigDecimal = new BigDecimal(value).setScale(4, RoundingMode.UP);

        return bigDecimal.doubleValue();
    }

    public static boolean doubleIsEqual(double a, double b) {

        return Math.abs(a - b) < 0.01;
    }
}